//
//  SISSTenantView.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/15.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSTenantView.h"
#import "SISSTitleView.h"
#import "SISSShowView.h"

@interface SISSTenantView ()

@property (weak, nonatomic)  UILabel *nameLabel;
@property (weak, nonatomic)  UILabel *houseTypeLabel;
@property (weak, nonatomic)  UILabel *budgetLabel;
@property (weak, nonatomic)  UILabel *regionAndDetailLabel;
@property (weak, nonatomic)  UILabel *releaseTimeLabel;
@property (weak, nonatomic)  UIView *rentalDemandView;
@property (strong, nonatomic) NSArray<UILabel *> *rentalDemandLabels;

@property (weak, nonatomic)  SISSTitleView *titleView;
@property (weak, nonatomic)  UIView *demandAllocationView;
@property (strong, nonatomic) NSArray<UILabel *> *demandAllocationLabels;
@property (weak, nonatomic)  UILabel *additionalNoteLabel;
@property (weak, nonatomic)  SISSShowView *residentsCountView;
@property (weak, nonatomic)  SISSShowView *intendCheckInTimeView;
@property (weak, nonatomic)  SISSShowView *intendDirectionView;
@property (weak, nonatomic)  SISSShowView *decorationView;

@end
@implementation SISSTenantView

- (void)setTenantModel:(SISSTenantModel *)tenantModel {
    _tenantModel = tenantModel;
    
    NSString *sexStr = tenantModel.sex == 1 ? @"先生" : (tenantModel.sex == 2 ? @"女士" : @"");
    self.nameLabel.text = self.isShowFullName ? tenantModel.renterName : [NSString stringWithFormat:@"%@%@  求租",[tenantModel.renterName substringToIndex:1],sexStr];
    self.houseTypeLabel.text = tenantModel.houseType;
    self.budgetLabel.text = [NSString stringWithFormat:@"%@-%@元",tenantModel.minPrice,tenantModel.maxPrice];
    self.regionAndDetailLabel.text = [NSString stringWithFormat:@"%@-%@ %@",tenantModel.region,tenantModel.regionDetail,tenantModel.neighbourhood ? [NSString stringWithFormat:@"| %@",tenantModel.neighbourhood] : @""];
    self.releaseTimeLabel.text = [NSDate yyr_stringToString:tenantModel.insertTime];
    self.additionalNoteLabel.text = tenantModel.otherdesc;
    self.residentsCountView.rightStr = [NSString stringWithFormat:@"%@人",tenantModel.residents];
    self.intendCheckInTimeView.rightStr = [NSDate yyr_stringToString:tenantModel.checkinTime];
    self.intendDirectionView.rightStr = tenantModel.direction;
    self.decorationView.rightStr = tenantModel.decoration;
    
    NSArray *houseFeatureArr = tenantModel.houseFeature.length == 0 ? @[] : [tenantModel.houseFeature componentsSeparatedByString:@"#"];
    
    for (NSInteger i = 0; i < houseFeatureArr.count ; i++) {
        UILabel *label = self.rentalDemandLabels[i];
        label.hidden = NO;
        label.text = houseFeatureArr[i];
        label.text = [NSString stringWithFormat:@" %@ ",houseFeatureArr[i]];
        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
    }
    WeakSelf
    [self.rentalDemandView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (tenantModel.houseFeature.length == 0) {
            make.height.mas_equalTo(1);
        } else {
            make.height.mas_equalTo(weakSelf.rentalDemandLabels[houseFeatureArr.count - 1].frame.origin.y + 30);
        }
    }];
    
     NSArray *houseConfigureArr = tenantModel.houseConfigure.length == 0 ? @[] : [tenantModel.houseConfigure componentsSeparatedByString:@"#"];
    CGFloat labelX = 0;
    CGFloat labelY = 0;
    CGFloat labelWidth = 80;
    for (NSInteger i = 0; i < houseConfigureArr.count ; i++) {
        UILabel *label = self.demandAllocationLabels[i];
        label.hidden = NO;
        label.text = houseConfigureArr[i];
        label.text = [NSString stringWithFormat:@" %@ ",houseConfigureArr[i]];
//        UIColor *color = [self matchHouseConfigureTextColorWithStr:houseConfigureArr[i]];
//        label.layer.borderColor = color.CGColor;
//        label.textColor = color;
        label.layer.backgroundColor = COLOR(0x84DFD8).CGColor;
        label.textColor = [UIColor whiteColor];
        if (labelX + labelWidth + 20 > SCREEN_WIDTH) {
            labelX = 0;
            labelY += 36;
        }
        labelWidth = [UILabel getWidthWithTitle:houseConfigureArr[i] font:[UIFont systemFontOfSize:15]] + 16;
        CGRect frame = CGRectMake(labelX, labelY, labelWidth, 28);
        label.frame = frame;
        
        labelX = CGRectGetMaxX(label.frame) + 8;
    }
    [self.demandAllocationView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (tenantModel.houseConfigure.length == 0) {
            make.height.mas_equalTo(1);
        } else {
            make.height.mas_equalTo(weakSelf.demandAllocationLabels[houseConfigureArr.count - 1].frame.origin.y + 30);
        }
    }];
}

- (void)setIsShowFullName:(BOOL)isShowFullName {
    _isShowFullName = isShowFullName;
    self.nameLabel.text = self.tenantModel.renterName;
}


- (UIColor *)matchHouseConfigureTextColorWithStr:(NSString *)str {
    UIColor *color;
    if ([str isEqualToString:@"宽带"]) {
        color = COLOR(0x84DFD8);
    } else if ([str isEqualToString:@"沙发"]) {
        color = COLOR(0x70D9AF);
    } else if ([str isEqualToString:@"床"]) {
        color = COLOR(0x94D879F);
    } else if ([str isEqualToString:@"衣柜"]) {
        color = COLOR(0x94D879);
    } else if ([str isEqualToString:@"冰箱"]) {
        color = COLOR(0x81D2DC);
    } else if ([str isEqualToString:@"空调"]) {
        color = COLOR(0x8EBDE4);
    } else if ([str isEqualToString:@"热水器"]) {
        color = COLOR(0x7A4D8F);
    } else if ([str isEqualToString:@"独立卫生间"]) {
        color = COLOR(0x94ABF0);
    } else if ([str isEqualToString:@"电梯"]) {
        color = COLOR(0x73D68F);
    } else if ([str isEqualToString:@"洗衣机"]) {
        color = COLOR(0x87D126);
    } else if ([str isEqualToString:@"阳台"]) {
        color = COLOR(0x91ACD0);
    }
    return color;
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel *nameLabel = [UILabel yyr_labelWithText:@"X先生/女士求租" fontSize:16 textColor:MAIN_TEXT_COLOR];
    
    [self addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UILabel *houseTypeLabel = [UILabel yyr_labelWithText:@"户型" fontSize:16 textColor:MAIN_TEXT_COLOR];
    [self addSubview:houseTypeLabel];
    self.houseTypeLabel = houseTypeLabel;
    
    UILabel *budgetLabel = [UILabel yyr_labelWithText:@"预算" fontSize:16 textColor:[UIColor redColor]];
    budgetLabel.font = [UIFont boldSystemFontOfSize:16];
    [self addSubview:budgetLabel];
    self.budgetLabel = budgetLabel;
    
    UILabel *regionAndDetailLabel = [UILabel yyr_labelWithText:@"意向区域 | 小区" fontSize:16 textColor:MAIN_TEXT_COLOR];
    [self addSubview:regionAndDetailLabel];
    self.regionAndDetailLabel = regionAndDetailLabel;
    
    UILabel *releaseTimeLabel = [UILabel yyr_labelWithText:@"发布时间" fontSize:16 textColor:MAIN_TEXT_COLOR];
    [self addSubview:releaseTimeLabel];
    self.releaseTimeLabel = releaseTimeLabel;
    
    UIView *rentalDemandView = [[UIView alloc] init];
    [self addSubview:rentalDemandView];
    self.rentalDemandView = rentalDemandView;
    
    NSMutableArray *tagLabelsArr = [NSMutableArray arrayWithCapacity:5];
    CGFloat labelX = 0;
    CGFloat labelY = 0;
    CGFloat labelWidth = 72;
    for (NSInteger i = 0; i < 6; i++) {
        if (labelX + labelWidth + 20 > SCREEN_WIDTH) {
            labelX = 0;
            labelY += 36;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, 28)];
        label.text = @"租房要求";
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.borderWidth = 1;
        label.layer.cornerRadius = 4;
        label.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
        label.layer.masksToBounds = YES;
        [rentalDemandView addSubview:label];
        
        labelX = CGRectGetMaxX(label.frame) + 10;
        label.hidden = YES;
        [tagLabelsArr addObject:label];
    }
    self.rentalDemandLabels = tagLabelsArr.copy;
    
    SISSTitleView *titleView = [[SISSTitleView alloc] init];
    titleView.titleStr = @"所需配置";
    [self addSubview:titleView];
    self.titleView = titleView;
    
    UIView *demandAllocationView = [[UIView alloc] init];
    [self addSubview:demandAllocationView];
    self.demandAllocationView = demandAllocationView;
    
    NSMutableArray *allocationLabelsArr = [NSMutableArray arrayWithCapacity:5];
    labelX = 0;
    labelY = 0;
    labelWidth = 66;
    for (NSInteger i = 0; i < 11; i++) {
        if (labelX + labelWidth + 20 > SCREEN_WIDTH) {
            labelX = 0;
            labelY += 36;
        }
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, 28)];
        label.text = @"所需配置";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15];
//        label.layer.borderWidth = 1;
        label.layer.cornerRadius = 4;
//        label.layer.masksToBounds = YES;
//        label.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
        [demandAllocationView addSubview:label];
        
        labelX = CGRectGetMaxX(label.frame) + 8;
        label.hidden = YES;
        [allocationLabelsArr addObject:label];
    }
    self.demandAllocationLabels = allocationLabelsArr.copy;
    
    UILabel *additionalNoteLabel = [UILabel yyr_labelWithText:@"补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明补充说明" fontSize:16 textColor:MAIN_TEXT_COLOR];
    [self addSubview:additionalNoteLabel];
    self.additionalNoteLabel = additionalNoteLabel;
    
    SISSShowView *residentsCountView = [[SISSShowView alloc] init];
    residentsCountView.isShowBottomLine = YES;
    residentsCountView.leftStr = @"居住人数：";
    residentsCountView.rightStr = @"";
    [self addSubview:residentsCountView];
    self.residentsCountView = residentsCountView;
    
    SISSShowView *intendCheckInTimeView = [[SISSShowView alloc] init];
    intendCheckInTimeView.isShowBottomLine = YES;
    intendCheckInTimeView.leftStr = @"预计入住时间：";
    [self addSubview:intendCheckInTimeView];
    self.intendCheckInTimeView = intendCheckInTimeView;
    
    SISSShowView *intendDirectionView = [[SISSShowView alloc] init];
    intendDirectionView.isShowBottomLine = YES;
    intendDirectionView.leftStr = @"意向朝向：";
    intendDirectionView.rightStr = @"";
    [self addSubview:intendDirectionView];
    self.intendDirectionView = intendDirectionView;
    
    SISSShowView *decorationView = [[SISSShowView alloc] init];
    decorationView.isShowBottomLine = YES;
    decorationView.leftStr = @"装修程度：";
    decorationView.rightStr = @"";
    [self addSubview:decorationView];
    self.decorationView = decorationView;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    WeakSelf
    CGFloat margin = 8.0;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(weakSelf).offset(3 * margin);
    }];
    
    [self.houseTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.nameLabel.mas_right).offset(margin);
        make.bottom.equalTo(weakSelf.nameLabel);
    }];
    
    [self.budgetLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.houseTypeLabel.mas_right).offset(2 * margin);
        make.centerY.equalTo(weakSelf.nameLabel);
    }];
    
    [self.regionAndDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.nameLabel);
        make.top.equalTo(weakSelf.nameLabel.mas_bottom).offset(2 * margin);
    }];
    
    [self.releaseTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf).offset(-2 * margin);
        make.centerY.equalTo(weakSelf.regionAndDetailLabel);
    }];
    
    [self.rentalDemandView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.nameLabel);
        make.top.equalTo(weakSelf.regionAndDetailLabel.mas_bottom).offset(2 * margin);
        make.right.equalTo(weakSelf).offset(-2 * margin);
        make.height.mas_equalTo(self.rentalDemandLabels.lastObject.frame.origin.y + 30);
    }];
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf.rentalDemandView.mas_bottom).offset(2 * margin);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.demandAllocationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.nameLabel);
        make.top.equalTo(weakSelf.titleView.mas_bottom).offset(2 * margin);
        make.right.equalTo(weakSelf).offset(-2 * margin);
        make.height.mas_equalTo(self.demandAllocationLabels.lastObject.frame.origin.y + 30);
    }];
    
    [self.additionalNoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.demandAllocationView.mas_bottom).offset(3 * margin);
        make.left.equalTo(weakSelf.nameLabel);
        make.right.equalTo(weakSelf).offset(-2 * margin);
    }];
    
    [self.residentsCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf.additionalNoteLabel.mas_bottom).offset(3 * margin);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.intendCheckInTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf.residentsCountView.mas_bottom);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.intendDirectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf.intendCheckInTimeView.mas_bottom);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.decorationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf);
        make.top.equalTo(weakSelf.intendDirectionView.mas_bottom);
        make.height.mas_equalTo(5 * margin);
        make.bottom.lessThanOrEqualTo(weakSelf);
    }];
}


@end
